<template>
  <div>
    <el-dialog :visible.sync="visible" width="500px" title="差异设置">
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="客户名称" v-if="type === 'customer'">
          <label>{{ form.customerNoLabel }}</label>
        </el-form-item>
        <el-form-item label="商品实测接受偏差率" prop="deviationRate">
          <el-input v-model.number="form.deviationRate"
                    class="form_input_default" placeholder="请输入正整数"
                    oninput="value=value.replace(/[^0-9]/g,'')">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="是否覆盖订单和商品信息" prop="isOverActual">
          <el-switch
            class="w_fill"
            v-model="form.isOverActual"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="Y"
            inactive-value="N">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">{{ $tt('label_cancel') }}</el-button>
        <el-button type="primary" v-if=" type === 'customer' " @click="sumbitByCustomers">{{
            $tt('label_confirm')
          }}
        </el-button>
        <el-button type="primary" v-else-if=" type === 'sku' " @click="sumbitSku">{{ $tt('label_confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "mms_sku_diff_setting",
  data() {
    return {
      type: '',
      visible: false,
      form: {
        customerNoLabel: '',
        deviationRate: '',
        isOverActual: 'N'
      },
      rows: [],
      rules: {
        deviationRate: {required: true, message: '请填写商品实测偏差率', trigger: 'blur'},
      },
    }
  },

  methods: {
    init() {
      this.rows = []
      this.form = {
        customerNoLabel: '',
        deviationRate: '',
        isOverActual: 'N'
      }
    },
    showByCustomerIndex(row) {

      this.init()

      this.type = 'customer'
      this.rows = [_.cloneDeep(row)]
      let label = ''
      for (let i = 0; i < this.rows.length; i++) {
        label += (this.rows[i].companyName + (i === this.rows.length - 1 ? '' : ', '))
      }
      this.form.customerNoLabel = label

      this.$ajax({
        url: "/mms-api/sku/CustomerSkuRuleRpc/detailByCustomerId.json?customerId=" + row.id,
        success: (responseData) => {
          if (responseData.data) {
            this.form.deviationRate = responseData.data.deviationRate
            this.form.isOverActual = responseData.data.isOverActual
          }
        }
      });
      this.visible = true
    },
    showByCustomers(rows) {
      this.init()
      this.type = 'customer'
      this.rows = _.cloneDeep(rows)
      let label = ''
      for (let i = 0; i < this.rows.length; i++) {
        label += (this.rows[i].companyName + (i === this.rows.length - 1 ? '' : ', '))
      }
      this.form.customerNoLabel = label
      this.visible = true
    },
    // 单独设置
    showBySkuIndex(row) {
      this.init()
      this.type = 'sku'
      this.$ajax({
        url: "/mms-api/sku/MmsSkuInfoRpc/detail.json?id=" + row.id,
        success: (responseData) => {
          if (responseData.data) {
            this.form.deviationRate = responseData.data.deviationRate
            this.form.isOverActual = responseData.data.isOverActual
          }
        }
      });

      this.rows.push(row)

      this.visible = true
    },
    // 批量处理
    showBySkus(rows) {
      this.init()
      this.type = 'sku'
      this.rows = _.cloneDeep(rows)

      this.visible = true
    },

    // 覆盖整体客户数据
    sumbitByCustomers() {
      if (this.$clickFast()) return
      this.$refs['form'].validate((valid, obj) => {
        if (valid) {
          let idList = []
          for (let i = 0; i < this.rows.length; i++) {
            idList.push(this.rows[i].id)
          }

          this.$ajax({
            url: "/mms-api/sku/CustomerSkuRuleRpc/batchSaveRule.json",
            type: 'json',
            data: {
              customerIds: idList,
              skuRuleDTO: {
                deviationRate: this.form.deviationRate,
                isOverActual: this.form.isOverActual
              }
            },
            success: (responseData) => {
              this.$message({
                showClose: true,
                message: this.$tt('tip_submit_success'),
                type: 'success'
              })
              this.visible = false
            }
          });
        } else {
          this.$message.error(this.$tt('form_validate_error'))
          return false;
        }
      });
    },
    sumbitSku() {
      if (this.$clickFast()) return
      this.$refs['form'].validate((valid, obj) => {
        if (valid) {
          let list = this.rows.map((item) => {
            return {
              id: item.id,
              deviationRate: this.form.deviationRate,
              isOverActual: this.form.isOverActual
            }
          })
          this.$ajax({
            url: '/mms-api/sku/MmsSkuInfoRpc/batchUpdate.json',
            type: 'json',
            data: {
              skuUpdateList: list
            },
            success: (responseData) => {
              this.$message({
                showClose: true,
                message: this.$tt('tip_submit_success'),
                type: 'success'
              })
              this.visible = false
            }
          });
        } else {
          this.$message.error(this.$tt('form_validate_error'))
          return false;
        }
      });
    }

  }

}
</script>

<style scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}

.el-icon-arrow-down {
  font-size: 12px;
}
</style>
